package cn.deschen.search.mysql.handlers.impl;

import cn.deschen.search.constants.OpType;
import cn.deschen.search.index.IndexHandler;
import cn.deschen.search.index.adRegion.AdRegionObject;
import cn.deschen.search.mysql.BinlogRowData;
import cn.deschen.search.mysql.handlers.BinlogRowDataHandler;
import cn.deschen.search.mysql.utils.SerializableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;

import static cn.deschen.search.constants.TableConstants.AdRegion.*;

/**
 * @Author hanbin_chen
 * @Description 地区处理器
 * @DateTime 2021/6/16
 * @Version V1.0.0
 */
@Component
@Slf4j
public class AdRegionHandler implements BinlogRowDataHandler {

    @Override
    public void handlerBinlogRowData(BinlogRowData rowData) {
        for (Map<String, Serializable> fieldValueMap : rowData.getRowDataList()) {
            AdRegionObject regionObject = new AdRegionObject();

            fieldValueMap.forEach((k, v) -> {
                switch (k) {
                    case COLUMN_ID:
                        regionObject.setId(SerializableUtil.parse2Long(v));
                        break;
                    case COLUMN_PROVINCE:
                        regionObject.setProvince(SerializableUtil.parse2String(v));
                        break;
                    case COLUMN_CITY:
                        regionObject.setCity(SerializableUtil.parse2String(v));
                        break;
                    case COLUMN_IS_DEL:
                        regionObject.setIsDel(SerializableUtil.parse2Integer(v));
                        break;
                }
            });
            IndexHandler.handlerLevel2Index(regionObject, OpType.to(rowData.getEventType()));
        }

    }
}
